package com.leicx.guli.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leicx.guli.common.constant.ParamConstant;
import com.leicx.guli.common.utils.CommonPageQueryUtil;
import com.leicx.guli.common.utils.PageUtils;
import com.leicx.guli.common.utils.Query;
import com.leicx.guli.product.dao.AttrGroupDao;
import com.leicx.guli.product.entity.AttrAttrgroupRelationEntity;
import com.leicx.guli.product.entity.AttrEntity;
import com.leicx.guli.product.entity.AttrGroupEntity;
import com.leicx.guli.product.service.AttrAttrgroupRelationService;
import com.leicx.guli.product.service.AttrGroupService;
import com.leicx.guli.product.service.AttrService;
import com.leicx.guli.product.vo.attr.AttrGroupWithAttrVo;
import com.leicx.guli.product.vo.sku.item.SkuItemVo;
import com.leicx.guli.product.vo.sku.item.SpuItemAttrGroupVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Resource
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(key)) {
            wrapper.and((obj) -> obj.eq("attr_group_id", key).or().like("attr_group_name", key));
        }
        if (Objects.nonNull(catId) && catId != 0) {
            wrapper.eq("catelog_id", catId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params), wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> attrRelationList(Long attrGroupId) {
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationService.listByAttrGroupId(attrGroupId);
        if (CollUtil.isNotEmpty(list)) {
            List<Long> collect = list.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            return attrService.list(new QueryWrapper<AttrEntity>().in(ParamConstant.ATTR_ID, collect));
        }
        return new ArrayList<>();
    }

    @Override
    public PageUtils attrNoRelationList(Long attrGroupId, Map<String, Object> params) {
        // 获取分类id
        AttrGroupEntity groupEntity = getById(attrGroupId);
        Long categoryId = groupEntity.getCatelogId();
        // 获取分类下的所有分组
        List<AttrGroupEntity> groupEntities = this.baseMapper.selectList(
                new QueryWrapper<AttrGroupEntity>().eq(ParamConstant.CATELOG_ID, categoryId));
        List<Long> groupIds = groupEntities.stream().map(AttrGroupEntity::getAttrGroupId).collect(Collectors.toList());
        // 获取分组关联的所有属性
        List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.listByAttrGroupIds(groupIds);
        List<Long> attrIdList = relationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>()
                // 当前分类下的
                .eq(ParamConstant.CATELOG_ID, categoryId)
                // 只选规格参数，不选销售属性
                .eq(ParamConstant.ATTR_TYPE, 1);
        if (CollUtil.isNotEmpty(attrIdList)) {
            // 将已经关联过的排除在外
            wrapper.notIn(ParamConstant.ATTR_ID, attrIdList);
        }
        // 拼接搜索条件
        CommonPageQueryUtil.setKeyQuery(wrapper, params, ParamConstant.ATTR_ID, ParamConstant.ATTR_NAME);
        IPage<AttrEntity> page = attrService.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrs(Long catLogId) {
        // 获取三级分类下的所有属性分组
        List<AttrGroupEntity> entities = this.list(new QueryWrapper<AttrGroupEntity>().eq(ParamConstant.CATELOG_ID, catLogId));

        return entities.stream()
                .map(q -> {
                    AttrGroupWithAttrVo vo = BeanUtil.copyProperties(q, AttrGroupWithAttrVo.class);
                    // 获取属性分组下的所有属性
                    List<AttrEntity> attrEntities = this.attrRelationList(vo.getAttrGroupId());
                    vo.setAttrs(attrEntities);
                    return vo;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
    }

}